package com.hyt.it.ogt.ykcj.common.exception;

import com.hyt.common.apiInfor.ApiResponse;

/**
 * 请求不合法异常类
 *
 * @author liuq
 * @since 2020-09-27
 */
@SuppressWarnings("serial")
public class InvalidRequestException extends RuntimeException {

    int code;

    public InvalidRequestException(int code) {
        super(String.valueOf(code).intern());
        this.code = code;
    }

    public InvalidRequestException(int code, String message) {
        super(message);
        this.code = code;
    }

    public int getCode() {
        return this.code;
    }

    /**
     * 获取响应封装
     *
     * @param <T>
     * @return
     */
    public <T> ApiResponse<T> response() {
        return ApiResponse.<T>builder()
                .code(getCode())
                .build();
    }

    /**
     * 获取异常
     *
     * @param code
     * @return
     */
    public static InvalidRequestException getException(Integer code) {
        return new InvalidRequestException(code);
    }

    /**
     * 获取异常
     *
     * @param code
     * @param message
     */
    public static InvalidRequestException getException(Integer code, String message) {
        return new InvalidRequestException(code, message);
    }

    /**
     * 抛出异常
     *
     * @param code
     */
    public static void throwException(Integer code) {
        throw new InvalidRequestException(code);
    }

    /**
     * 抛出异常
     *
     * @param code
     * @param message
     */
    public static void throwException(Integer code, String message) {
        throw new InvalidRequestException(code, message);
    }
}
